<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace BI\Controller;
use Common\Controller;
/**
 * Description of AnalyzeDigController
 * 当需要进行数据挖掘时，执行此类
 *
 * @author coach
 */
class AnalyzeDigController extends CommonController {
    //put your code here
    private $AxisConfig=array();
    private $param=array();

    public function index(){
        $this->display();
    }
    
    public function getDigDataResults(){
        $post=I('post.'); 
       
        set_time_limit(0);
        
        $code = empty($post['code'])?'Spaceplus':$post['code'];
        $isCache=0;
        
        $AnalyzeData=new \Common\Controller\ConfigureController($code,$isCache);
        
        $AxisConfig=$AnalyzeData->initBaseConfig();
        
        $this->AxisConfig=$AxisConfig['Axis'];
      
        $this->setPostParam($post);
        
        $this->param['code'] = $code;
        $this->param['type'] = 9;
        
        $this->setDigModel($post['t']);

        //var_dump($this->param);
        //$AnalyzeData=new \Common\Controller\BdataResultController($this->param);
        //dump($this->param);exit();
        $AnalyzeData=new \Common\Controller\ReturnResultController($this->param);
        $data=$AnalyzeData->getAnalyzeDataResults();
        //dump($this->param);
        $this->behavior($this->param); 
        $this->ajaxReturn($data);
    }
    
    /**
     * @方法：判断适应哪种挖掘
     * @param type $post
     */
    private function setDigModel($model){
        switch ($model){
            case 1://按项目
            default :
                $this->param['yAxis'] = array('lpmc'=>array(0=>'1',1=>''));
                $this->param['vAxis'] = array('CJJE'=>array(0=>'sum',1=>'2'),'JZMJ'=>array(0=>'sum',1=>'5'),'JMDJ'=>array(0=>'avg',1=>'7'),'TS'=>array(0=>'sum',1=>'10'));
                $this->param['ranking'] = array('topNum'=>5,'topColumn'=>array('CJJE','SUM','DESC'));
                break;
            case 2://按业态
                $this->param['yAxis'] = array('yt'=>array(0=>'1',1=>''));
                $this->param['vAxis'] = array('CJJE'=>array(0=>'sum',1=>'2'),'JZMJ'=>array(0=>'sum',1=>'5'),'JMDJ'=>array(0=>'avg',1=>'7'),'TS'=>array(0=>'sum',1=>'10'));
                $this->param['ranking'] = array('topNum'=>20,'topColumn'=>array('CJJE','SUM','DESC'));
                break;
            case 3://按月份
                $this->param['yAxis'] = array('ym'=>array(0=>'1',1=>''));
                $this->param['vAxis'] = array('CJJE'=>array(0=>'sum',1=>'2'),'JZMJ'=>array(0=>'sum',1=>'5'),'JMDJ'=>array(0=>'avg',1=>'7'),'TS'=>array(0=>'sum',1=>'10'));
                $this->param['ranking'] = array('topNum'=>5,'topColumn'=>array('CJJE','SUM','DESC'));
                break;
        }
    }


    /**
     * @解析传入参数
     * @param type $post
     */
    private function setPostParam($post){
    
        foreach ($post as $key=>$val){
            if(is_array($var)){
                if(!empty($val)){
                    $this->param['where'][$key]=$val;
                }                
            }else{
                
                if(!empty($val)){
                   
                    if(strpos($val,'@')){//存在多个维度值时
                        $k = explode(',', $key);
                        $v = explode('@', $val);
                        foreach ($k as $index=>$values){
                            $key=  strtoupper($values);
                            $this->setWhereString($key,$v[$index]);
                        }
                    }else{//单一值时
                        if($key!='type'){
                            $this->setWhereString($key,$val);
                        }                        
                    }
                }                
            }
        }
    }
    
    /**
     * 
     * @param type $key
     * @param type $val 字符串
     */
    private function setWhereString($key,$val){
        switch ($this->AxisConfig[$key]['FieldType']){
            case 1://对照
                $this->param['where'][$key]=$val;
                break;
            case 3://统计
                unset($this->param['where'][$key]);

                if(is_string($val)){//存在多个维度值时
                    $reg = explode('-', $val);
                    $this->param['where'][$key][]=$reg[0];
                    $this->param['where'][$key][]=$reg[1];
                }elseif(is_array($val)){
                    $this->param['where'][$key][]=$val[0];
                    $this->param['where'][$key][]=$val[1];
                }                
                break;
            case 2://日期
              
                $this->setDateValue($key,$val);//返回条件组合
                break;
            case 4://描述
                $this->param['where'][$key]=$val;
                break;
        }
    }


    private function setDateValue($key,$val){
        //获取日期型字段的基础表字段名
        $DataColumn = $this->AxisConfig[$key]['DataColumn'];
        //
        unset($this->param['where'][$DataColumn]);
        
        switch ($this->AxisConfig[$key]['Expression']){
            case 'Y'://年度
                $year = mb_substr($val,0,4,'utf-8');
                $this->param['where'][$key] = $year;
                break;
            case 'YM':
                $year = mb_substr($val,0,4,'utf-8');
                //echo $year;
                //$date = explode('-', $val);
                $this->param['where']['YY'] = $year;//$date[0];
                
                $month = mb_substr($val,5,2,'utf-8');
                $this->param['where']['YM'] = $month;//$date[1];
                break;
            case 'YW':
                    //$year = mb_substr($val,0,10,'utf-8');
                    //$month = mb_substr($val,5,2,'utf-8');
                    //echo $month;exit();
                $date = explode('至', $val);
                $this->param['where'][$DataColumn][] = $date[0];
                $this->param['where'][$DataColumn][] = $date[1];
                break;
            case 'YQ':
                $year = mb_substr($val,0,4,'utf-8');
               
               // $date = explode('-', $val);
                $this->param['where']['YY'] = $year;//$date[0];
                
                $q = mb_substr($val,6,1,'utf-8');
                
                $this->param['where']['YQ'] = $q;//$date[1];
                break;
            case 'YB':
                //$date = explode('-', $val);
                $year = mb_substr($val,0,4,'utf-8');//$date[0];
                
                $left = mb_substr($val,5,3,'utf-8');//$date[1];
                
                if($left==1){
                    $this->param['where'][$DataColumn][] = $year.'-01-01';
                    $this->param['where'][$DataColumn][] = $year.'-06-30';
                }else{
                    $this->param['where'][$DataColumn][] = $year.'-07-01';
                    $this->param['where'][$DataColumn][] = $year.'-12-31';
                }
                break;
            case 'D':
            default :
                $this->param['where'][$DataColumn] = $val;
                break;
        }
    }
    
    //数据导出
    public function export(){
        $data= (array)json_decode(urldecode(I('request.data')));

        $title=array_shift($data['data']);
        $name=$data['name'];

        exportExcel($data['data'],$name,$title);
    }

}
